package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.exception.AmountIsZeroException;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.net.www.ApplicationLaunchException;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper unserMapper;
    /**
     * 查看工作台菜品总览
     * @return
     */
    @Override
    public DishOverViewVO selectDish() {
        //已起售数量
        int sold = 0;
        //已停售数量
        int discontinued = 0;
        //直接查询所有的菜品
        List<Dish> dishes = dishMapper.selectAll();
        for (Dish dish : dishes) {
            if (dish.getStatus().equals( StatusConstant.DISABLE)){
                discontinued++;
            }else {
                sold++;
            }
        }
        return new DishOverViewVO(sold,discontinued);
    }
    /**
     * 查看套餐的总览数量
     * @return
     */
    @Override
    public DishOverViewVO selectSetmeal() {
        //已起售数量
        int sold = 0;
        //已停售数量
        int discontinued = 0;
        //查询所有的套餐表
        List<Setmeal> setmeals = setmealMapper.selectAll();
        //遍历套餐,查看套餐状态进行登记
        for (Setmeal setmeal : setmeals) {
            if (setmeal.getStatus().equals(StatusConstant.DISABLE)){
                discontinued++;
            }else {
                sold++;
            }
        }
        return new DishOverViewVO(sold,discontinued);
    }
    /**
     * 查看订单的数量
     * @return
     */
    @Override
    public OrderOverViewVO selecOrders() {
        //待接单数量 2
        int waitingOrders = 0;
        //待派送数量 3
        int deliveredOrders = 0;
        //已完成数量 5
        int completedOrders = 0;
        //已取消数量 6
        int cancelledOrders = 0;
        //全部订单
        int allOrders = 0;

        LocalDateTime beginTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        Map<String,Object> map = new HashMap<>();
        map.put("beginTime",beginTime);
        map.put("endTime",endTime);
        map.put("status",null);
        //查看所有的订单信息
        List<Orders> orders = ordersMapper.getByMap(map);

        //判断订单里的订单状态
        for (Orders order : orders) {
            if (order.getStatus().equals(Orders.TO_BE_CONFIRMED)){
                waitingOrders++;
            }else if (order.getStatus().equals(Orders.CONFIRMED)) {
                deliveredOrders++;
            }else if (order.getStatus().equals(Orders.COMPLETED)) {
                completedOrders++;
            } else if (order.getStatus().equals(Orders.CANCELLED)) {
                cancelledOrders++;
            }
                allOrders++;
        }
        return new OrderOverViewVO(waitingOrders,deliveredOrders,completedOrders,cancelledOrders,allOrders);
    }

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime todayStart,LocalDateTime todayEnd) {
        BusinessDataVO businessData = new BusinessDataVO();
        //新增用户数
        //设置当天的开始时间和结束时间
        Integer count = unserMapper.selectAll(todayStart,todayEnd);
        //查看当天符合要求的订单
        List<Orders> orders = ordersMapper.selectUserCount(todayStart,todayEnd);
        if (orders.size() == 0 || orders == null) {
            businessData.setValidOrderCount(0);
            businessData.setTurnover(0.0);
            businessData.setNewUsers(0);
            businessData.setOrderCompletionRate(0.0);
            businessData.setUnitPrice(0.0);
            return businessData;
        }
        //当天有多少新增用户就进行赋值
        businessData.setNewUsers(count);
        //订单完成率
        //查看订单一共有多少单，在查看订单已完成有多少单
        //一共有size单
        Integer size = orders.size();
        //定义有效单数
        Integer single = 0;
        for (Orders order : orders) {
            //已完成的单量
            if (order.getStatus().equals(Orders.COMPLETED)) {
                single++;
            }
        }
        //每天的营业额
        Double dou = ordersMapper.sum(todayStart,todayEnd);

        double orderCompletionRate = single.doubleValue() / size;

        if (single == 0.0 ) {
            orderCompletionRate = 0.0;
        }

        businessData.setOrderCompletionRate(orderCompletionRate);
        if (dou == null || dou == 0){
            //如果dou等于null,说明今天没有已完成的订单，金额是0
            dou=0.0;
        }
        //营业额
        businessData.setTurnover(dou);
        //平均客单价
        //总金额除总单量
        double pj = dou / size;
        businessData.setUnitPrice(pj);
        //有效订单数
        businessData.setValidOrderCount(single);
        return businessData;
    }

}
